package com.book.bridge.function;

import com.alibaba.fastjson.JSONObject;
import com.book.pojo.UserInfo;
import com.book.service.UserInfoService;
import com.book.utils.HttpClientUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Component
@Slf4j
public class RegisterLoginByGitee extends AbstractRegisterLoginFun implements RegisterLoginFuncInterface{

    @Value("${gitee.state}")
    private String giteeState;

    @Value("${gitee.token.url}")
    private String giteeTokenUrl;

    @Value("${gitee.user.url}")
    private String giteeUserUrl;

    @Value("${gitee.user.prefix}")
    private String giteeUserPrefix;
    @Autowired
    UserInfoService userInfoService;
    /**
     * 登录 重构 ，之前的逻辑直接拿过来
     * @param account
     * @param password
     * @return
     */
    @Override
    public String login(String account, String password) {
        return commonLogin(account,password,userInfoService);

    }

    /**
     * 注册 重构 之前的逻辑直接拿过来
     * @param userInfo
     * @return
     */
    @Override
    public String register(UserInfo userInfo) {
        return commonRegister(userInfo,userInfoService);

    }

    /**
     * 存在 重构
     * @param username
     * @return
     */
    @Override
    public boolean checkUserExists(String username) {
        return commonCheckUserExists(username,userInfoService);
    }

    @Override
    public String login3rd(HttpServletRequest request) {
        String code = request.getParameter("code");
//        String state = request.getParameter("state");

        //        if (!giteeState.equals(state)) {
//            throw new UnsupportedOperationException("InValid state");
//        }
        // 请求gitee平台获取token，并携带code
        String tokenUrl = giteeUserUrl.concat(code);
        // 获取token
        JSONObject tokenResponse = HttpClientUtils.execute(tokenUrl, HttpMethod.POST);
        String accessToken = String.valueOf(tokenResponse.get("access_token"));
        log.info("用户Token {}",accessToken);
        // 获取用户信息
        String userUrl = giteeTokenUrl.concat(accessToken);
        JSONObject userInfoResponse = HttpClientUtils.execute(userUrl, HttpMethod.GET);
        log.info("用户信息 {}",userInfoResponse);
        String username = giteeUserPrefix.concat(userInfoResponse.getString("name"));
        String password = username;
        // 自动注册和登录
        return autoRegister3rdAndLogin(username,password);
    }



    private String autoRegister3rdAndLogin(String username, String password) {
        if (userInfoService.checkUserExists(username)) {
            return userInfoService.login(username, password);
        }
        UserInfo userInfo = new UserInfo().setUsername(username)
                .setUserPassword(password)
                .setUserEmail(username)
                .setCreateDate(new Date())
                ;
        userInfoService.save(userInfo);
        return userInfoService.login(username, password);
    }

}
